import { Injectable } from '@angular/core';
import { Http, Headers } from "@angular/http";
import { CalculateExpression } from "../entity/calculate-expression.model";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/subject";
import { AppConfig } from "../config";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class CalculateExpressionService {
  private readonly param = "ElectricCalExpression";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) { }

  // 请求表达式列表
  getExpressionList(): Promise<CalculateExpression[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.json().data as CalculateExpression[])
      .catch(this.handleError);
  }

  // 根据风场id查询线路；
  getExpressionByFarmId(farmId: number, phaseId: number): Promise<CalculateExpression[]> {
    const url = `${this.url}/farmId/${farmId}/phaseId/${phaseId}`;
    return this.http.get(url)
      .toPromise()
      .then(response => {
        return response.json().data as CalculateExpression[];
      })
      .catch(this.handleError);
  }


  // 根据工期id查询线路；
  getExpressionByPhaseId(id: number): Promise<CalculateExpression[]> {
    const url = `${this.url}/phaseId/${id}`;
    return this.http.get(url)
      .toPromise()
      .then(response => response.json().data as CalculateExpression[])
      .catch(this.handleError);
  }

  // 创建表达式
  create(expression: CalculateExpression): Promise<CalculateExpression> {
    return this.http
      .post(this.url, JSON.stringify(expression))
      .toPromise()
      .then(res => res.json().data)
      .catch(this.handleError);
  }

  update(turbStatus: CalculateExpression): Promise<CalculateExpression> {
    const url = `${this.url}`;
    return this.http
      .put(url, JSON.stringify(turbStatus))
      .toPromise()
      .then(() => turbStatus)
      .catch(this.handleError);
  }

  //根据farmCode判断是否已经存在
  hasExpression(expression: string, farmId: number, phaseId: number, categoryId: number): Observable<boolean> {

    const url = `${this.url}/farmId/${farmId}/phaseId/${phaseId}/categoryId/${categoryId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }
    }).catch(this.handleError);
  }

  // 删除
  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  // 批量线路删除
  batchDelete(keyIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${keyIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return true;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

}



// WEBPACK FOOTER //
// ./src/app/service/calculate-expression.service.ts